var app = new Vue({
    el: '#Receive',
    data () {
        return {
            receiverName: '', // 收货人姓名
            mobile: '', // 收货人手机号
            sex: 'MALE',
            houseEstate: '点击选择',
            fullAddress: '',
            addressOne: null,
            addressTwo: '',
            btnDisabled: false,
            showAdd: false
        }
    },
    
    methods: {
        // 返回上一页
        goback () {
            window.history.go(-1)
        },
        // 搜索小区弹窗展开
        showAddress () {
            this.showAdd = true
        },
        // 搜索小区弹窗关闭
        changeShow (data) {
            this.showAdd = data
        },
        // 获取小区数据
        setHouseEstate (data) {
            this.houseEstate = data.houseEstate
            this.addressOne = data.addressOne
            this.fullAddress = data.fullAddress
        },
        // 保存地址提交
        submit () {
            if (!this.receiverName) {
                this.toast = this.$createToast({
                    txt: '请填写收货人的姓名！',
                    type: 'txt'
                })
                this.toast.show()
                return
            }
            if (!this.mobile) {
                this.toast = this.$createToast({
                    txt: '请填写收货人手机号码！',
                    type: 'txt'
                })
                this.toast.show()
                return
            }
            if (!this.checkMobile(this.mobile)) {
                this.toast = this.$createToast({
                    txt: '请填写正确的手机号码！',
                    type: 'txt'
                })
                this.toast.show()
                return
            }
            if (!this.addressOne) {
                this.toast = this.$createToast({
                    txt: '请填写收货地址！',
                    type: 'txt'
                })
                this.toast.show()
                return
            }
            if (!this.addressTwo) {
                this.toast = this.$createToast({
                    txt: '请填写门牌号！',
                    type: 'txt'
                })
                this.toast.show()
                return
            }
            Axios.post('user/address/addReceiveAddress.do', {
                receiverName: this.receiverName,
                addressOne: this.addressOne,
                addressTwo: this.addressTwo,
                sex: this.sex,
                mobile: this.mobile
            }).then(res => {
                if (res.respCode === 'S') {
                    this.toast = this.$createToast({
                        txt: '添加地址成功！',
                        type: 'txt'
                    })
                    this.toast.show()
                    setTimeout(() => {
                        window.location.href = 'my_shouhuo.html'
                    }, 1500)
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                }
            })
        },
        // 验证手机号码
        checkMobile(key) {
            const reg = /^((1[3-9][0-9])+\d{8})$/
            return reg.test(key)
        }
    }
})